package org.lzfto.flink.demo.business.flinkTest.base;

import org.apache.commons.lang3.ObjectUtils;
import org.lzfto.flink.demo.business.flinkTest.base.db.BasicJdbcTable;
import org.lzfto.flink.demo.business.flinkTest.base.db.CustomJdbcTable;
import org.lzfto.flink.demo.business.flinkTest.base.db.StandardJdbcTable;
import org.lzfto.flink.demo.business.flinkTest.base.file.ExcelSourceTable;
import org.lzfto.flink.demo.config.AppConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 逻辑表基类，主要作用:
 * <pre>
 *   1.从配置文件config.json获取表格信息
 *   2.构建Flink create table所需要的with语句
 * </pre>
 */
public class BasicTable implements BasicTableConnectorOperation {
	
	private static final  Logger LOGGER = LoggerFactory.getLogger(BasicTable.class);
	private static String TABLE_DIRECTION_SOURCE = "source";
	private static String TABLE_DIRECTION_TARGET = "target";
	public static String TABLE_TYPE_JDBC = "jdbc";
	public static String TABLE_TYPE_EXCEL = "excel";
		

	private String type;
	private String env;
	private String tableName;
	private String connector;
	private String comments;

	public String getEnv() {
		return env;
	}

	public void setEnv(String env) {
		this.env = env;
	}

	public String getConnector() {
		return connector;
	}

	public void setConnector(String connector) {
		this.connector = connector;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getComments() {
		return comments;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}

	public static BasicTable getSourceJdbcTable(String tableName) {
		return getJdbcTable(TABLE_TYPE_JDBC, TABLE_DIRECTION_SOURCE, tableName);
	}

	public static BasicTable getTargetJdbcTable(String tableName) {
		return getJdbcTable(TABLE_TYPE_JDBC, TABLE_DIRECTION_TARGET, tableName);
	}

	public static BasicTable getSourceExcelTable(String tableName) {
		return getExcelTable(tableName);
	}

	public static BasicTable getExcelTable(String tableName) {
		if (ObjectUtils.isEmpty(AppConfiguration.getConf().getTables())) {
			return null;
		}

		String env = AppConfiguration.getConf().getEnv();
		for (BasicTable tab : AppConfiguration.getConf().getTables()) {
			if (tab.getType().equals(TABLE_TYPE_EXCEL)) {
				ExcelSourceTable table = (ExcelSourceTable) tab;
				if ( (table.getEnv().equals("*") || table.getEnv().equals(env) ) && table.getTableName().equals(tableName)) {
					ExcelSourceTable newTable = new ExcelSourceTable();
					newTable.setType(tab.getType());
					newTable.setEnv(table.getEnv());
					newTable.setPath(table.getPath());
					newTable.setFileName(table.getFileName());
					newTable.setDeleteAfterRead(table.getDeleteAfterRead());
					newTable.setStartRow(table.getStartRow());
					newTable.setExtension(table.getExtension());
					newTable.setExtensionHyphen(table.getExtensionHyphen());
					return newTable;
				}
			}
		}
		throw new RuntimeException("没有找到表:" + tableName + "(env=" + env + ")");
	}

	private static BasicTable getJdbcTable(String type, String direction, String tableName) {
		if (ObjectUtils.isEmpty(AppConfiguration.getConf().getTables())) {
			return null;
		}
		String env = AppConfiguration.getConf().getEnv();
		for (BasicTable tab : AppConfiguration.getConf().getTables()) {
			if (tab.getType().equals(TABLE_TYPE_JDBC)) {
				BasicJdbcTable table = (BasicJdbcTable) tab;
				String runEnv=AppConfiguration.getConf().getEnv();
				if ((table.getEnv().equals("*") || table.getEnv().equals(runEnv)) && table.getDirection().equals(direction)
						&& table.getTableName().equals(tableName)) {
					BasicJdbcTable newTable = null;
					if (table instanceof StandardJdbcTable) {
						StandardJdbcTable tmpTable = new StandardJdbcTable();
						newTable = tmpTable;
					} else if (table instanceof CustomJdbcTable custJdbcTable) {
						CustomJdbcTable tmpTable = new CustomJdbcTable();
						tmpTable.setDriverClass(custJdbcTable.getDriverClass());
						tmpTable.setIdColCode(custJdbcTable.getIdColCode());
						newTable = tmpTable;

					}
					else {
						LOGGER.warn("对于{}类型{}没有匹配的配置类",table.getTableName(),table.getType());
						continue;
					}
					newTable.setType(table.getType());
					newTable.setEnv(table.getEnv());
					newTable.setDirection(table.getDirection());
					newTable.setUrl(table.getUrl());
					newTable.setTableName(tableName);
					newTable.setUserName(table.getUserName());
					newTable.setPassword(table.getPassword());
					newTable.setComments(table.getComments());
					System.out.println("找到表-env=" + env + ",direction=" + direction + ",name=" + tableName);
					return newTable;
				}
			}
		}
		throw new RuntimeException("没有找到表:" + tableName + "(env=" + env + ",direction=" + direction + ")");
	}

	public BasicTable withConnector(String connector) {
		this.setConnector(connector);
		return this;
	}

	@Override
	public String build() throws Exception {
		return null;
	}
}
